package com.ecar.api.service.order.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecar.api.service.driver.DriverService;
import com.ecar.api.service.order.PaymentCommentService;
import com.ecar.common.dto.DataBean;
import com.ecar.common.util.StringUtil;
import com.ecar.core.driver.dao.DriverMapper;
import com.ecar.core.driver.entity.Driver;
import com.ecar.core.passenger.dao.PaymentCommentMapper;
import com.ecar.core.passenger.entity.PaymentComment;

import net.sf.json.JSONObject;

@Service
@Transactional
public class PaymentCommentServiceImpl implements PaymentCommentService {

	@Autowired
	PaymentCommentMapper paymentCommentMapper;
	
	@Autowired
	DriverMapper driverMapper;
	
	@Override
	public DataBean rate(PaymentComment paymentComment) {
		if(StringUtil.isEmpty(paymentComment.getOrderNo())){return new DataBean(500,new JSONObject(),"参数错误");}
		
		PaymentComment temp=paymentCommentMapper.selectByOrderNo(paymentComment.getOrderNo());
		if(temp==null){
			return new DataBean(404,new JSONObject(),"该订单尚未支付");
		}
		if(temp.getPayStatus()==2){
			return new DataBean(403,new JSONObject(),"支付失败，无权评论订单");
		}
		PaymentComment t=new PaymentComment();
		t.setId(temp.getId());
		t.setCommentContent(paymentComment.getCommentContent());
		t.setCommentType(paymentComment.getCommentType());
		t.setRate(paymentComment.getRate());
		int pk=paymentCommentMapper.updateByPrimaryKeySelective(t);
		if(pk<=0){
			return new DataBean(500,new JSONObject(),"系统错误，评论失败");
		}
		
		double rate=paymentCommentMapper.getRateByDriverId(temp.getDriverId());
		Driver record=new Driver();
		record.setId(temp.getDriverId());
		record.setRates(rate);
		driverMapper.updateByPrimaryKeySelective(record);
		
		
		return new DataBean(200,new JSONObject(),"评论成功");
	}
	
	

}
